Module netutils
[hide private]

Module netutils

source code

This library can be used to retrieve network and TCP information, such as getting the Operating system name, checking if a MAC Address is valid, check if an IP Address is valid etc.

Classes [hide private]
  ProtocolType
  _PortType
Identify port type in application layer of OSI
  _PortTypeEnum
  Endpoint
  ConnectivityEndpoint
  BaseEndpointBuilder
Base builder for endpoint as we have two types: URI and IP service endpoint
  UriEndpointBuilder
  ServiceEndpointBuilder
  EndpointReporter
  DNSResolver
Class responsible for getting nslookup results on a client's machine
  IpResolver
Class responsible for resolving IP addresses on probe machine's side
  ResolveException
Base exception class used for DNS resolving
  BaseDnsResolver
Base class for DNS resolvers
  FallbackResolver
Implementation of DNS resolving using fallback approach against different resolvers
  DnsResolverByShell
  JavaDnsResolver
DNS Resolver that uses java API - InetAddress
  __IPProtocols
  __DomainScopeManager
Functions [hide private]
string
getHostAddress(dnsName, defaultValue=None)
Resolves the host name to the IP Address in DNS table.
source code
string
getHostName(ipAddress, defaultValue=None)
Resolves the IP address to the host name in the DNS table.
source code
Boolean
isValidIp(ipAddr)
Checks whether the given IP address is valid.
source code
string
convertAsciiCodesToHex(asciiSeq)
Converts chars in given string from hex codes to ascii symbols e.g.
source code
string
parseMac(origmac)
Parses the given macAddress and converts it to the system format XXXXXXXXXXXX, where X is an uppercase hexadecimal digit.
source code
integer
isValidMac(mac, ifType='ethernetCsmacd')
Checks whether the specified MAC address is valid.
source code
string
parseNetMask(origmask)
Parses the supplied network mask into the common format of %d.%d.%d.%d
source code
string
getOSName(client, cmd)
Retrieves the operating system name.
source code
int
isLoopbackIp(ip)
Checks whether the specified IP is loopback (assumes the given IP is *valid*).
source code
int
isLocalIp(ip)
Checks whether the specified IP is local (assumes the given IP is *valid*).
source code
 
isPrivateIp(ip)
Checks is the specified IP belongs to private network (assumes the given IP is *valid*).
source code
 
getAvailableProtocols(Framework, PROT_TYPE, IP, DOMAIN=None)
Returns available protocols of desired type defined in domain scope document for concrete IP
source code
 
resolveFQDN(shell, ip)
Resolves fqdn of a host by ip.
source code
 
resolveIP(shell, hostName)
Resolves ip address of a host by its name(dns) NsLookup is used first and then destinations' hosts file on fallback Shell, str -> str or None
source code
 
isUrlAvailable(url, acceptedStatusCodeRange, timeout=10000)
Checks whether url is available str, list(str), int -> bool
source code
 
doHttpGet(url, timeout=20000, requestedData='body', headerName=None)
Performs HTTP(S) Connection to the specified URL
source code
 
checkTcpConnectivity(ipAddress, portNumber, timeout)
Checks the TCP connection to the given ipAddress on the given port.
source code
 
pingIp(Framework, ipAddress, timeout)
Ping the specified device
source code
 
isIpBroadcast(ipAddress, netmask)
Checks whether the given IP is a broadcast IP
source code
 
isIpAnycast(ipAddress, interfaceName)
Checks whether the given IP is anycast Anycast is a network addressing and routing scheme whereby data is routed to the "nearest" or "best" destination as viewed by the routing topology Can be obtained in following way: non-local IP on loop-back interface
source code
 
getPortDescription(portNumber, portType)
Return port name for the given port number and type.
source code
 
getDBConnection(userName, password, driverName, connectionURL)
Return PyConnection to the database (see Jython's zxJDBC description)
source code
integer
getLeadingOnesCount(number)
Returns the count of 1 in binary representation of given number
source code
integer
getShortMask(netmask)
Returns the count of set bit in given network mask e.g.
source code
string
decodeSubnetMask(routingPrefix, isIpV4=1)
Returns: subnet mask
source code
string
obtainDotDecimalTuple(cidrNotationBlock)
Returns: subnet mask in dot-decimal notation or None
source code
integer
convertIpToInt(ipString)
Transforms IP address from string representation to numeric
source code
str
getLowestIp(listIp)
Getting minimal IP from list of IPs
source code
integer
negateNetMask(mask)
Basing on integer representation of network mask returns MAXIMAL count of IPs which can be addressed in given network
source code
 
getNetworkClassByNetworkMask(networkMask) source code
 
getNetworkClassByNetworkPrefix(networkPrefix) source code
 
createTcpEndpoint(address, port, portType=None) source code
 
updateEndpointAddress(endpoint, address) source code
 
createDefaultFallbackResolver(shell=None) source code
 
createDnsResolverByShell(shell, dnsServerAddress=None)
Factory method to create DNS resolver
source code
Variables [hide private]
  _IGNORED_INTERFACE_MACS = "204153594EFF", "33506F453030", "505...
  _IGNORED_INTERFACE_MAC_PATTERNS = '^0{6}', '0{6}$', 'F{12}', '...
  PortTypeEnum = _PortTypeEnum(HTTP= _PortType('http'), HTTPS= _...
  WINDOWS_HOSTS_CONFIG = '%SystemRoot%\system32\drivers\etc\hosts'
  UNIX_HOSTS_CONFIG = '/etc/hosts'
  IPPROTOCOLS = __IPProtocols(JIPProtocols)
  DOMAIN_SCOPE_MANAGER = __DomainScopeManager(JDomainScopeManager)
Function Details [hide private]

getHostAddress(dnsName, defaultValue=None)

source code 

Resolves the host name to the IP Address in DNS table. On failure to resolve the name, the default value parameter is returned.

Parameters:
  • dnsName (string) - host name to resolve
  • defaultValue - the value to return if the host name cannot be resolved
Returns: string
IP address of the resolved host name

getHostName(ipAddress, defaultValue=None)

source code 

Resolves the IP address to the host name in the DNS table. On failure to resolve the address the , default value parameter is returned.

Parameters:
  • ipAddress (string) - IP address to resolve
  • defaultValue - the value to return if the address cannot be resolved
Returns: string
host name of the resolved IP address

isValidIp(ipAddr)

source code 

Checks whether the given IP address is valid.

Parameters:
  • ipAddr (string) - IP address to check
Returns: Boolean
true if the IP is valid, else false

convertAsciiCodesToHex(asciiSeq)

source code 

Converts chars in given string from hex codes to ascii symbols e.g. '4A415641' -> 'JAVA'

Parameters:
  • asciiSeq - hex string for conversion
Returns: string

Note: Don't be frustrated by the name of function! '4A415641' - is ascii codes; returned value 'JAVA' - is a hex string

parseMac(origmac)

source code 

Parses the given macAddress and converts it to the system format XXXXXXXXXXXX, where X is an uppercase hexadecimal digit.

Parameters:
  • origmac (string) - raw or formated MAC address
Returns: string
parsed MAC address in the converted format
Raises:
  • ValueError - If address has invalid format

isValidMac(mac, ifType='ethernetCsmacd')

source code 

Checks whether the specified MAC address is valid.

Parameters:
  • mac (string) - MAC address to check
Returns: integer
1 if MAC is valid, 0 otherwise

parseNetMask(origmask)

source code 

Parses the supplied network mask into the common format of %d.%d.%d.%d

Parameters:
  • origmask (string) - NetMask address to parse
Returns: string
parsed NetMask address

getOSName(client, cmd)

source code 

Retrieves the operating system name.

Parameters:
  • client (shellClinet) - pre-connected shell client
Returns: string
operating system name

isLoopbackIp(ip)

source code 

Checks whether the specified IP is loopback (assumes the given IP is *valid*). Loopback IPs are any IP which starts with '127.'

Parameters:
  • ip (String) - IP address to check
Returns: int
1 if the IP is loopback, else 0

isLocalIp(ip)

source code 

Checks whether the specified IP is local (assumes the given IP is *valid*). Local IPs are any IP which starts with '127.' or '0.'

Parameters:
  • ip (String) - IP address to check
Returns: int
1 if the IP is local, else 0

isPrivateIp(ip)

source code 

Checks is the specified IP belongs to private network (assumes the given IP is *valid*).
Private IP addresses described in RFC 1918 (Private Address Space section)
They are:
    10.0.0.0    - 10.255.255.255  (10/8 prefix)
    172.16.0.0    - 172.31.255.255  (172.16/12 prefix)
    192.168.0.0    - 192.168.255.255 (192.168/16 prefix)
@param  ip: IP address to check
@type     ip: String
@return: 1 if the IP belongs to private network, else 0
@rtype: int

resolveFQDN(shell, ip)

source code 

Resolves fqdn of a host by ip. NsLookup is used first and then @netutils.getHostName used on fallback

Shell, ip -> str or None

doHttpGet(url, timeout=20000, requestedData='body', headerName=None)

source code 

Performs HTTP(S) Connection to the specified URL

Returns data according to the requestedData flag:
  'body': Full Response Body as String
  'header': Returns the response header with the specified headerName

checkTcpConnectivity(ipAddress, portNumber, timeout)

source code 

Checks the TCP connection to the given ipAddress on the given port.

Parameters:
  • ipAddress (String) - IP address of the remote computer to check
  • portNumber (int) - The port number to check
  • timeout (int @return 1 if the TCP connection succeeded, else) - connection timeout in millisecondes

pingIp(Framework, ipAddress, timeout)

source code 

Ping the specified device

Parameters:
  • ipAddress (string) - the IP address to ping
  • timeout (int) - ping timeout in milliseconds
Returns:
1 if the machine is pingable, else 0

isIpBroadcast(ipAddress, netmask)

source code 

Checks whether the given IP is a broadcast IP

Parameters:
  • ipAddress (string) - IP address to check
  • netmask (string) - corresponding IP network mask
Returns:
boolean

isIpAnycast(ipAddress, interfaceName)

source code 

Checks whether the given IP is anycast Anycast is a network addressing and routing scheme whereby data is routed to the "nearest" or "best" destination as viewed by the routing topology Can be obtained in following way: non-local IP on loop-back interface

Parameters:
  • ipAddress (string) - IP address to check
  • interfaceName (string) - name of the network interface

getPortDescription(portNumber, portType)

source code 

Return port name for the given port number and type.

Parameters:
  • portNumber - The port number
  • portType - The port type (TCP / UDP)
Returns:
String the description

getDBConnection(userName, password, driverName, connectionURL)

source code 

Return PyConnection to the database (see Jython's zxJDBC description)

Parameters:
  • userName - the username to connect to DB with
  • password - the password to connect to DB with
  • driverName - name of the driver to connect through
Returns:
com.ziclix.python.sql.PyConnection

getLeadingOnesCount(number)

source code 

Returns the count of 1 in binary representation of given number

Parameters:
  • number (integer)
Returns: integer

getShortMask(netmask)

source code 

Returns the count of set bit in given network mask e.g. for mask '255.255.255.0' return value will be 24

Parameters:
  • netmask (string)
Returns: integer

decodeSubnetMask(routingPrefix, isIpV4=1)

source code 
Parameters:
  • routingPrefix - routing prefix - natural numbers
  • isIpV4 - if true dot-decimal mask will be used instead of IpV6
Returns: string
subnet mask

Precondition: IpV6 is not supported Decode routing prefix to IpV4 (dotDecimal) mask or IpV6 mask

obtainDotDecimalTuple(cidrNotationBlock)

source code 
Parameters:
  • cidrNotationAddress - address string in CIDR notation (xxx.xxx.xxx.xxx/xx)
Returns: string
subnet mask in dot-decimal notation or None

Precondition: only IPv4 is supported Obtain subnet mask from cidr notation.

convertIpToInt(ipString)

source code 

Transforms IP address from string representation to numeric

Parameters:
  • ipString (string) - given IP or network mask to transform
Returns: integer

getLowestIp(listIp)

source code 

Getting minimal IP from list of IPs

Parameters:
  • listIp (list(str)) - list of IPs from which need to find minimal
Returns: str
Raises:
  • ValueError - list of IPs is empty

Note: if list of local IPs passed return the latest one

negateNetMask(mask)

source code 

Basing on integer representation of network mask returns MAXIMAL count of IPs which can be addressed in given network

Parameters:
  • mask (integer) - network mask in integer representation
Returns: integer

See Also: convertIpToInt


Variables Details [hide private]

_IGNORED_INTERFACE_MACS

Value:
"204153594EFF", "33506F453030", "505054503030", "444553540000", "44455\
3544200", "001986002B48", "020054554E01", "7A7700000001", "BAD0BEEFFAC\
E", "00F1D000F1D0", "80000404FE80", "000613195800", "7A8020000200", "F\
EFFFFFFFFFF", "028037EC0200", "8000600FE800", "0200CAFEBABE", "0200547\
46872", "000FE207F2E0", "020255061358", "080058000001", "000419000001"\
, "002637BD3942", "025041000001", "009876543210", "582C80139263", "00A\
DE1AC1C1A", "02004C4F4F50", "444553547777"

_IGNORED_INTERFACE_MAC_PATTERNS

Value:
'^0{6}', '0{6}$', 'F{12}', '^00FF[\dA-F]{8}', '^0250F200000[\dA-F]', '\
^000AFA020[\dA-F]00', '^00E00900000[\dA-F]', '^02BF[\dA-F]{8}', '^5455\
434452[\dA-F]{2}', '^020000000[\dA-F]{3}', '^00A0D5FFFF[\dA-F]{2}', '^\
005056C0000[\dA-F]', '^00059A3C7[\dA-F]00', '^001C4200000[\dA-F]'

PortTypeEnum

Value:
_PortTypeEnum(HTTP= _PortType('http'), HTTPS= _PortType('https'), SNMP\
= _PortType('snmp'), SMTP= _PortType('smpt'))